#coding=utf-8

import urllib2


def send_request():
    headers = {
            "User-Agent" : "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36",
            "Connection": "keep-alive"}
    #构建一个包含请求包头的请求对象
    request = urllib2.Request("http://www.baidu.com/", headers=headers)

    """
    Request(url, data=None, headers=None)
    这里url是请求地址
    data:提交的form表单数据,同时http请求方法将从默认的GET方式改为POST方式
    headers参数为字典类型,包含了需要发送的http报头的键值对
    """

    #发送指定的url地址请求,返回一个类文件的响应对象
    response = urllib2.urlopen(request)
    #读取响应中内容,获取网页原始编码字符串
    html = response.read()

    return html

if __name__ == '__main__':
    html = send_request()

    with open('baidu.html', 'w') as f:
        f.write(html)

